package com.yike.product.controller;

import com.yike.product.param.GoodsCategoryParam;
import com.yike.product.service.GoodsCategoryService;
import com.yike.product.vo.GoodsCategoryTreeVo;
import com.yike.product.vo.GoodsCategoryVo;
import lombok.extern.slf4j.Slf4j;
import com.yike.product.param.GoodsCategoryPageParam;
import com.yike.common.base.BaseController;
import com.yike.common.api.ApiResult;
import com.yike.common.pagination.Paging;
import com.yike.common.log.Module;
import com.yike.common.log.OperationLog;
import com.yike.common.enums.OperationLogType;
import com.yike.common.api.Add;
import com.yike.common.api.Update;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类 控制器
 *
 * @author Sakura
 * @since 2024-10-18
 */
@Slf4j
@RestController
@RequestMapping("/platform/goodsCategory")
@Module("product")
@Api(value = "商品分类API", tags = {"平台商品分类"})
public class GoodsCategoryController extends BaseController {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    /**
     * 添加商品分类
     */
    @PostMapping("/add")
    @OperationLog(name = "添加商品分类", type = OperationLogType.ADD)
    @ApiOperation(value = "添加商品分类", response = ApiResult.class)
    public ApiResult<Boolean> addGoodsCategory(@Validated(Add.class) @RequestBody GoodsCategoryParam goodsCategoryParam) throws Exception {
        boolean flag = goodsCategoryService.saveGoodsCategory(goodsCategoryParam);
        return ApiResult.result(flag);
    }

    /**
     * 修改商品分类
     */
    @PostMapping("/update")
    @OperationLog(name = "修改商品分类", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改商品分类", response = ApiResult.class)
    public ApiResult<Boolean> updateGoodsCategory(@Validated(Update.class) @RequestBody GoodsCategoryParam goodsCategoryParam) throws Exception {
        boolean flag = goodsCategoryService.updateGoodsCategory(goodsCategoryParam);
        return ApiResult.result(flag);
    }

    /**
     * 删除商品分类
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除商品分类", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除商品分类", response = ApiResult.class)
    public ApiResult<Boolean> deleteGoodsCategory(@PathVariable("id") Long id) throws Exception {
        boolean flag = goodsCategoryService.deleteGoodsCategory(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取商品分类详情
     */
    @GetMapping("/info/{id}")
    //@OperationLog(name = "商品分类详情", type = OperationLogType.INFO)
    @ApiOperation(value = "商品分类详情", response = GoodsCategoryVo.class)
    public ApiResult<GoodsCategoryVo> getGoodsCategory(@PathVariable("id") Long id) throws Exception {
        GoodsCategoryVo goodsCategoryVo = goodsCategoryService.getGoodsCategory(id);
        return ApiResult.ok(goodsCategoryVo);
    }

    /**
     * 商品分类分页列表
     */
    @PostMapping("/getPageList")
    //@OperationLog(name = "商品分类分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "商品分类分页列表", response = GoodsCategoryVo.class)
    public ApiResult<Paging<GoodsCategoryVo>> getGoodsCategoryPageList(@Validated @RequestBody GoodsCategoryPageParam goodsCategoryPageParam) throws Exception {
        Paging<GoodsCategoryVo> paging = goodsCategoryService.getGoodsCategoryPageList(goodsCategoryPageParam);
        return ApiResult.ok(paging);
    }

    @GetMapping("tree")
    //@OperationLog(name = "分类树", type = OperationLogType.INFO)
    @ApiOperation(value = "分类树", response = GoodsCategoryTreeVo.class)
    public ApiResult<List<GoodsCategoryTreeVo>> tree() throws Exception {
        List<GoodsCategoryTreeVo> goodsCategoryTreeVos = goodsCategoryService.tree();
        return ApiResult.ok(goodsCategoryTreeVos);
    }

}

